package cn.gs.exchanger;

// @author gs
import java.util.List;

public class ExchangerException extends Exception {

    public static final ExchangerException EMPTY_EXCEPTION = new ExchangerException(null, null, null);

    public static enum Type {

        EXECUTE_ERR,
        SERVER_NOT_EXIST, SERVER_NOT_RESPONSE,
        IO_ERROR, COMMAND_SYNTAX_ERROR,
        ARGS_ERR, UNKNOWN_EXCEPTION;

        public ExchangerException create(String msg, Throwable cause) {
            return new ExchangerException(msg, this, cause);
        }
    }
    private Type type;
    private String message;
    private Throwable cause;

    public ExchangerException(String msg, Type type, Throwable cause) {
        super(type + ":" + msg, cause);
        this.type = type;
        this.message = type + ":" + msg;
        this.cause = cause;
    }

    private Throwable inner(List<String> es, int index) {
        if (es.size() > index) {
            Throwable th = inner(es, index + 2);
            Exception ex;
            if (es.size() > index + 1) {
                ex = new Exception(es.get(index) + es.get(index + 1), th);
            } else {
                ex = new Exception(es.get(index), th);
            }
        }
        return null;
    }

    public ExchangerException(List<String> es) {
        if (es.size() > 0) {
            this.type = Type.valueOf(es.get(0));
            if (es.size() > 1) {
                this.message = es.get(1);
                this.cause = inner(es, 2);
            }
        }
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Throwable getCause() {
        return cause;
    }

    public void setCause(Throwable cause) {
        this.cause = cause;
    }
}
